/**
 * Created by MD-3 on 2017/5/25.
 */


function initMngWindow(thisitem) {
    var thisitem = $(thisitem)
    var togstate = thisitem.attr("togstate")
    if("1" == togstate){
        thisitem.attr("togstate","0");
        return;
    }else {
        thisitem.attr("togstate","1");
    }

    $.ajax({
        type : "POST",
        url : "mngwindow.json",
        data : {},
        dataType: 'json',
        success : function(data) {
            if( 1 != data.action){
                return;
            }
            console.log("@log-initMngWindow:" + data.deviceList+","+data.alarmList);

            getAllDeviceHtml(data.deviceList);       //设备列表

            //getSelDeviceHtml();

            getAlarmHisHtml(data.alarmList);

        }
    });

}

function getAllDeviceHtml(deviceList) {
    var allDeviceUl = $('#myalldevice-ul');
    var htmlStr1 = "";
    for (var i in deviceList) {
        htmlStr1 += getMngDeviceLi(deviceList[i]);
    }
    allDeviceUl.html(htmlStr1);
}

function getSelDeviceHtml() {
    var selDeviceUl = $('#myseldevice-ul');
    var htmlStr2 = getSelDeviceLi();
    selDeviceUl.html(htmlStr2);
}

function getAlarmHisHtml(alarmList) {
    var selDeviceUl = $('#myalarmhis-ul');
    var htmlStr = "";
    for (var i in alarmList) {
        htmlStr += getMngAlarmLi(alarmList[i]);
    }
    selDeviceUl.html(htmlStr);
}

function getMngDeviceLi(mngDevice) {
    var childDevList = mngDevice.childDevList;

    var html = "";
    for(var i in childDevList) {
        html += getMngDevU2(childDevList[i]);
    }

    return getMngDevU1(mngDevice) +
            html
        + '   </div>'
        + '</div>';
}

//终端
function getMngDevU2(childDev) {
    var id          = childDev.id;
    var type        = childDev.type;
    var name        = childDev.name;
    var power       = childDev.power;
    var workState   = childDev.workState;
    var alarmState  = childDev.alarmState;

    var resultWS = getMngDevImg(workState, alarmState);
    var imgWorkS = resultWS.img;
    var titleWorkS = resultWS.title;
    var resultPower = getImgByPower(power);
    var imgPower = resultPower.img;
    var titlePower= resultPower.title;

    var htmlStr = '<div class="alldev-mngdevli">'
        +'    <span>' + id + '</span>'
        +'    <a onclick="mapPanTo(this)" deviceid=' + id + '>' + name + '</a>'
        +'    <img src="'+imgWorkS+'" title="'+titleWorkS+'"></img>'
        +'    <img src="'+imgPower+'" title="'+titlePower+'"></img>'
        +'</div>';
    return htmlStr;
}

//中继
function getMngDevU1(mngDevice){
    var id          = mngDevice.id;
    var type        = mngDevice.type;
    var name        = mngDevice.name;
    var power       = mngDevice.power;
    var workState   = mngDevice.workState;

    var resultWS = getMngDevImg(workState);
    var imgWorkS = resultWS.img;
    var titleWorkS = resultWS.title;
    var resultPower = getImgByPower(power);
    var imgPower = resultPower.img;
    var titlePower= resultPower.title;

    var id_MngDevUl = "mngdevul_"+id;
    var htmlStr =
        '<div class="alldev-mngdev" data-toggle="collapse" data-target="#'+id_MngDevUl+'">'
        +'    <a onclick="mapPanTo(this)" deviceid="' + id + '">' + name + '</a>'
        +'    <img src="'+imgWorkS+'" title="'+titleWorkS+'"></img>'
        +'    <img src="'+imgPower+'" title="'+titlePower+'"></img>'
        +'    <span class="caret mymng-caret"></span>'
        + '</div>'
        + '<div id="'+id_MngDevUl+'" class="collapse in alldev-mngdevul">'
        + '    <div class="list-group">';
    return htmlStr;
}

function  getMngDevImg(workState, alarmState) {
    var img = "";
    var title = "";
    if(alarmState && "1"==alarmState){
        img = resource_mock.IMG_DEVICE_WS2;
        title = "报警";
    } else if(workState && "1"==workState) {
        img = resource_mock.IMG_DEVICE_WS1;
        title = "离线";
    } else if(workState && "0"==workState) {
        img = resource_mock.IMG_DEVICE_WS0;
        title = "正常";
    }
    return {"img":img, "title":title};
}

function getImgByPower(power) {
    if(!power){
        return;
    }
    power = (power)/10;
    var img = "";
    var title = "";
    switch (power) {
        case 1 :
        case 0 :
            img = resource_mock.IMG_DEVICE_Power;
            title = "电量不足";
            break;
        default :
            break;
    }
    return {"img":img, "title":title};
}





function getMngAlarmLi(mngAlarm) {
    var id         = mngAlarm.id;
    var name       = mngAlarm.name;
    var alarmId    = mngAlarm.alarmId;
    var alarmTime  = mngAlarm.alarmTime;
    var alarmState = mngAlarm.alarmState;
    var alarmType  = mngAlarm.alarmType;
    var audioUrl   = mngAlarm.audioUrl;

    var mediaUrlStr = '<span>---</span>';
    if(audioUrl && ""!=audioUrl){
        mediaUrlStr = '<span><img onclick="playAlarmMedia(this)" src="'+resource_mock.IMG_ALARM_OFF
            +'" audioUrl=' + audioUrl + ' count="0"/></span>';
    }
    var alarmCheckStr = '---';
    if ("1" == alarmState) {
        alarmCheckStr = '<button type="button" class="btn btn-primary btn-xs" itemid="'+id+'" onclick="openDevice(this)">处理</button>';
    }else{
        alarmCheckStr = '<span>' + alarmType + '</span>'
    }
    var htmlStr = '<li>'
        + '<span>' + alarmId + '</span>'
        + '<a onclick="mapPanTo(this)" deviceid=' + id + '>' + name + '</a>'
        + '<span>' + alarmTime + '</span>'
        + mediaUrlStr
        + alarmCheckStr
        + '</li>';
    console.log("getMngAlarmLi:" + "--------" + audioUrl);
    return htmlStr;
}

function openDevice(thisitem){
    var thisitem = $(thisitem);
    var deviceid = thisitem.attr("itemid");
    var alarmCheck = "1";
    openDeviceInfo(deviceid,alarmCheck)
}



///////////////////////////////////////////////////////////////////////////////////

function selectDevice() {
    var detectorEle = document.getElementById('detector');
    var repeaterEle = document.getElementById('repeater');
    var selWorkstateEle = $('#mysel-workstate');

    var detectorChecked = detectorEle.checked == true ? "1" : "-1";    //0-中继，1-终端
    var repeaterChecked = repeaterEle.checked == true ? "0" : "-1";
    var selWorkstate = selWorkstateEle.val();
    var selAlarmstate = "0";
    if(selWorkstate == "2"){
        selWorkstate  = "9";
        selAlarmstate = "1";
    } else if(selWorkstate == "9"){
        selAlarmstate = "9";
    }
    //selWorkstate: 0-正常，1-离线，9-选择所有
    //selAlarmstate：1-有报警，9-选择所有
    console.log("@logo-selectDevice:"+"终端:" + detectorChecked + "--中继:" + repeaterChecked + "--工作：" + selWorkstate + "--报警：" + selAlarmstate)

    showSelectedDevice(detectorChecked, repeaterChecked, selWorkstate, selAlarmstate);
}

function showSelectedDevice(detectorChecked, repeaterChecked, selWorkstate, selAlarmstate) {
    if (!detectorChecked || !repeaterChecked || !selWorkstate || !selAlarmstate) {
        return;
    }
    for (var i = 0; i < deviceMarkerList.length; i++) {
        var device = deviceMarkerList[i];
        if ((device.type == repeaterChecked) &&
            (selWorkstate == "9" || device.workState == selWorkstate) &&
            (selAlarmstate == "9" || device.alarmState == selAlarmstate)) {
            device.marker.show();
        } else {
            device.marker.hide();
        }
        for(var j=0; j<deviceMarkerList[i].childDevlist.length; j++){
            var childDev = deviceMarkerList[i].childDevlist[j];
            if ((childDev.type == detectorChecked ) &&
                (selWorkstate == "9" || childDev.workState == selWorkstate) &&
                (selAlarmstate == "9" || childDev.alarmState == selAlarmstate)) {
                childDev.marker.show();
            } else {
                childDev.marker.hide();
            }
        }
    }
}



function mapPanTo(thisitem) {
    var link = $(thisitem);
    var deviceid = link.attr("deviceid");
    var pisotion = getDevById(deviceid).marker.getPosition();
    map.panTo(pisotion);

    //展开信息窗口
    openDeviceInfo(deviceid)
}



//搜索设备Id或设备名称
function mymngSchDevEnter() {
    console.log("@log-searchDeviceEnter");
    var e = window.event || arguments.callee.caller.arguments[0];         //火狐识别不了event
    if (e && e.keyCode == "13") {         //keyCode=13是回车键
        mymngSchDev();
    }
}

function mymngSchDev() {
    var keyword = document.getElementById("mymng-dev-keyword").value;
    console.log("@log-searchDevice-keyword:"+keyword);

    if(!keyword){   return;}
    var flag = searchDevice(keyword);
}


